package com.suixun.queue;

import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.suixun.entity.DeviceMessage;
import com.suixun.lib.httpsqs4j.HttpsqsClient;
import com.suixun.lib.httpsqs4j.HttpsqsException;

/**
 * 
 * 功能描述:  发送设备消息
 * @author jacky
 * @date 2018年1月23日下午3:04:07
 */
public class DeviceDeal {
	
	private final static Logger logger = LoggerFactory.getLogger(DeviceDeal.class);

	private final static String queueName = "Mqttcomm";
	@Resource
    private HttpsqsClient syssqs;

	/**
	 * 
	 * 功能描述: 发送设备指令
	 * @param deviceMessage
	 * @throws JsonProcessingException
	 * @throws HttpsqsException
	 */
    public void sendInstructions(DeviceMessage deviceMessage) {
    	Map<String, String> map = new HashMap<String, String>();
		map.put("msgid", deviceMessage.getCmd());
		map.put("mtdid", deviceMessage.getImei());
		map.put("msg", deviceMessage.getPacket());
		ObjectMapper mapper = new ObjectMapper();
        mapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
        String jsonStr;
		try {
			jsonStr = mapper.writeValueAsString(map);
			syssqs.putString(queueName, jsonStr);
		} catch (JsonProcessingException e) {
			logger.error("DeviceDeal->sendInstructionsErr", e);
		} catch (HttpsqsException e) {
			logger.error("DeviceDeal->sendInstructionsErr", e);
		}
    }
}
